package com.mvc.controller;

import com.customAnnotations.ch01.CurrentUser;
import com.customAnnotations.ch01.UserInfo;
import com.mvc.exception.AssertUtil;
import com.mvc.exception.BusinessException;
import com.mvc.exception.ParameterException;
import com.mvc.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/***
 *
 * 测试
 * @author wind
 * @since 2021-01-27
 */
@Slf4j
@RestController
@RequestMapping("/test")
public class MyTestController {

    @Autowired
    private StudentService studentService;

    @RequestMapping("/testParameterException")
    public String testParameterException(String target) throws Exception {
        AssertUtil.assertNull(target,"target 不能为空！");
        return "";
    }

    @RequestMapping("/testBusinessException")
    public String testBusinessException(@RequestParam("target") String target) throws Exception {
        if("1".equals(target)){
            throw new BusinessException();
        }
        return "SUCCESS";
    }

    @RequestMapping("/helloWorld")
    public String testHelloWorld() {
        return "index";
    }

    @RequestMapping("/testUser")
    public String testUser(@CurrentUser UserInfo userInfo){
        log.debug("=============={}",userInfo.getId());
        return "OK";
    }

    @RequestMapping("/student")
    public String getStudent(){
        studentService.getStudent();
        return HttpStatus.OK.getReasonPhrase();
    }
}
